﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.IO;
using System.Configuration;
using System.Web;

namespace Nha_Hang_Khach_San.Web
{
    public class SoDoPhong
    {
        public string MaPhong{get; set; }
        public string TenPhong{get; set; }
        public int TrangThai{get; set; }
        public string MaNhomPhong{get; set; }
        public string MaKhuVuc{get; set; }
        public string MauNen{get; set; }
        //public string DuongVien{get; set; }
    }

    public class SoDoKhuVuc
    {
        public string MaKhuVuc { get; set; }
        public string TenKhuVuc{get; set; }
    }

    public class ImgList
    {
        public int ImgIndex{get; set; }
        public string ImgUrl{get; set; }
    }

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "NHKSService" in code, svc and config file together.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class NHKSService : INHKSService
    {
        #region Updload & dowload
        public bool Upload(PictureFile picture)
        {
            FileStream fileStream = null;
            BinaryWriter writer = null;
            string filePath;
            try
            {
                filePath = HttpContext.Current.Server.MapPath(".") + ConfigurationManager.AppSettings["PictureUploadDirectory"] + picture.PictureName;
                //
                if (picture.PictureName != string.Empty)
                {
                    fileStream = File.Open(filePath, FileMode.Create);
                    writer = new BinaryWriter(fileStream);
                    writer.Write(picture.PictureStream);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
                if (writer != null)
                    writer.Close();
            }
        }

        public PictureFile Dowload(string pictureName)
        {
            FileStream fileStream = null;
            BinaryReader reader = null;
            string imagePath;
            byte[] imageBytes;

            try
            {
                imagePath = HttpContext.Current.Server.MapPath(".") + ConfigurationManager.AppSettings["PictureUploadDirectory"] + pictureName;
                if (File.Exists(imagePath))
                {
                    fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
                    reader = new BinaryReader(fileStream);
                    //
                    imageBytes = reader.ReadBytes((int)fileStream.Length);
                    return new PictureFile()
                    {
                        PictureName = pictureName,
                        PictureStream = imageBytes
                    };
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region So do phong
        public List<SoDoKhuVuc> GetDSKhuVuc()
        {
            var dsKhuVucs = new List<SoDoKhuVuc>();
            NHKSDataDataContext nhksData = new NHKSDataDataContext();
            var khuvucs = from kv in nhksData.KHUVUCs
                          select kv;
            foreach (var kvs in khuvucs)
            {
                var temp = new SoDoKhuVuc();
                temp.MaKhuVuc = kvs.MaKhuVuc;
                temp.TenKhuVuc = kvs.TenKhuVuc;
                dsKhuVucs.Add(temp);
            }
            return dsKhuVucs;
        }

        public List<SoDoPhong> GetDSPhong()
        {
            var dsPhongs = new List<SoDoPhong>();
            NHKSDataDataContext nhksData = new NHKSDataDataContext();
            var khuvucs = from kv in nhksData.KHUVUCs
                          select kv;
            foreach (var kvs in khuvucs)
            {
                var phongs = from p in nhksData.PHONGs
                             where p.MaKhuVuc == kvs.MaKhuVuc
                             select p;
                foreach (var ph in phongs)
                {
                    var temp = new SoDoPhong();
                    temp.MaPhong = ph.MaPhong;
                    temp.TenPhong = ph.TenPhong;
                    temp.TrangThai = Convert.ToInt32(ph.TinhTrang);
                    if (temp.TrangThai == 0)//Con trong
                    {
                        temp.MauNen = "Green";
                    }
                    else if (temp.TrangThai == 1)//Da o
                    {
                        temp.MauNen = "Red";
                    }
                    else if(temp.TrangThai==2)//Da dat
                    {
                        temp.MauNen = "Blue";
                    }
                    else if(temp.TrangThai==3)//Dang sua
                    {
                        temp.MauNen = "Yellow";
                    }
                    else if(temp.TrangThai==4)//Chua don
                    {
                        temp.MauNen = "Brown";
                    }
                    temp.MaNhomPhong = ph.MaNhomPhong;
                    //var nps = from np in nhksData.NHOMPHONGs
                    //          where np.MaNhomPhong == temp.MaNhomPhong
                    //          select np;
                    //foreach (var nphongs in nps)
                    //{
                    //    if (nphongs.TenNhomPhong == "Phòng đơn")
                    //    {
                    //        temp.DuongVien = "Black";
                    //    }
                    //    else if (nphongs.TenNhomPhong == "Phòng đôi")
                    //    {
                    //        temp.DuongVien = "Yellow";
                    //    }
                    //}
                    temp.MaKhuVuc = ph.MaKhuVuc;
                    dsPhongs.Add(temp);
                }
            }
            return dsPhongs;
        }

        public List<SoDoPhong> GetPhongTrong(DateTime ngay)
        {
            var dsPhongs = new List<SoDoPhong>();
            NHKSDataDataContext nhksData = new NHKSDataDataContext();

            return dsPhongs;
        }

        public List<ImgList> GetDSHinhAnh(string tenPhong)
        {
            int index = 0;
            List<ImgList> imageList = new List<ImgList>();
            NHKSDataDataContext nhksData = new NHKSDataDataContext();
            //Lay ma nhom phong
            var phongs = from phong in nhksData.PHONGs
                         where phong.TenPhong == tenPhong
                         select phong;
            foreach (var p in phongs)
            {
                var nhomphongs = from nhomphong in nhksData.NHOMPHONGs
                                 where nhomphong.MaNhomPhong == p.MaNhomPhong
                                 select nhomphong;
                foreach (var hinhanhs in nhomphongs)
                {
                    var has = from ha in nhksData.DANHSACHHINHs
                              where ha.MaLoaiHinh == hinhanhs.MaLoaiHinh
                              select ha;
                    foreach (var temp in has)
                    {
                        ImgList img = new ImgList();
                        img.ImgIndex = index++;
                        img.ImgUrl = temp.TenFile;
                        imageList.Add(img);
                    }
                }
            }
            return imageList;
        }

        public string getFirstImage(string tenphong)
        {
            string fileName = "";
            NHKSDataDataContext nhksData = new NHKSDataDataContext();
            //Lay ma nhom phong
            var phongs = from phong in nhksData.PHONGs
                         where phong.TenPhong == tenphong
                         select phong;
            foreach (var p in phongs)
            {
                var nhomphongs = from nhomphong in nhksData.NHOMPHONGs
                                 where nhomphong.MaNhomPhong == p.MaNhomPhong
                                 select nhomphong;
                foreach (var hinhanhs in nhomphongs)
                {
                    var has = from ha in nhksData.DANHSACHHINHs
                              where ha.MaLoaiHinh == hinhanhs.MaLoaiHinh
                              select ha;
                    foreach (var temp in has)
                    {
                        fileName = temp.TenFile;
                        break;
                    }
                }
            }
            return fileName;
        }

        public string GetLoaiPhong(string tenPhong)
        {
            string loai = "";
            NHKSDataDataContext nhksData = new NHKSDataDataContext();
            //Lay ma nhom phong
            
            //Lay ma nhom phong
            var phongs = from phong in nhksData.PHONGs
                         where phong.TenPhong == tenPhong
                         select phong;
            foreach (var p in phongs)
            {
                //lay mo ta
                var nhomphongs = from nhomphong in nhksData.NHOMPHONGs
                                 where nhomphong.MaNhomPhong == p.MaNhomPhong
                                 select nhomphong;
                foreach (var lp in nhomphongs)
                {
                    var loaiphongs=from lps in nhksData.LOAIPHONGs
                                   where lps.MaLoaiPhong==lp.MaLoaiPhong
                                   select lps;
                    foreach (var temp in loaiphongs)
                    {
                        loai = temp.TenLoaiPhong;
                    }
                }
            }

            return loai;
        }

        public string GetDonGiaPhong(string tenPhong)
        {
            string dongia = "";
            NHKSDataDataContext nhksData = new NHKSDataDataContext();
            var phongs = from phong in nhksData.PHONGs
                         where phong.TenPhong == tenPhong
                         select phong;
            foreach (var p in phongs)
            {
                //lay mo ta
                var nhomphongs = from nhomphong in nhksData.NHOMPHONGs
                                 where nhomphong.MaNhomPhong == p.MaNhomPhong
                                 select nhomphong;
                foreach (var temp in nhomphongs)
                {
                    string[] dg = Convert.ToString(temp.DonGia).Split('.');
                    dongia = dg[0];
                }
            }
            return dongia;
        }

        public string GetNhomPhong(string tenPhong)
        {
            string np = "";
            NHKSDataDataContext nhksData = new NHKSDataDataContext();
            var phongs = from phong in nhksData.PHONGs
                         where phong.TenPhong == tenPhong
                         select phong;
            foreach (var p in phongs)
            {
                //lay mo ta
                var nhomphongs = from nhomphong in nhksData.NHOMPHONGs
                                 where nhomphong.MaNhomPhong == p.MaNhomPhong
                                 select nhomphong;
                foreach (var temp in nhomphongs)
                {
                    np = temp.TenNhomPhong;
                }
            }
            return np;
        }

        public string GetMoTaPhong(string tenPhong)
        {
            string moTa = "";
            NHKSDataDataContext nhksData = new NHKSDataDataContext();
            //Lay ma nhom phong
            var phongs = from phong in nhksData.PHONGs
                         where phong.TenPhong == tenPhong
                         select phong;
            foreach (var p in phongs)
            {
                //lay mo ta
                var nhomphongs = from nhomphong in nhksData.NHOMPHONGs
                                 where nhomphong.MaNhomPhong == p.MaNhomPhong
                                 select nhomphong;
                foreach (var temp in nhomphongs)
                {
                    moTa = temp.MoTa;
                }
            }
            return moTa;
        }
        #endregion
        
    }
}
